package com.dingding.activity.core.activity.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.activity.core.activity.dao.IActivityRuleDao;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.activity.ActivityRefRule;
import com.dingding.common.core.cache.CacheConstants;
import com.dingding.common.core.cache.base.JedisUtil;
import com.dingding.common.core.cache.enums.DbEnum;
import com.dingding.common.core.util.SerializableUtil;

/**
 * 
 * @Description: 活动规则DAO
 * @author CHEHUANBO
 * @date 2016年12月7日
 * @since V2.6.2
 * 
 */
@Component("activityRuleDao")
public class ActivityRuleDao implements IActivityRuleDao, IActivityRuleCacheDao {

	private static final Logger log = Logger.getLogger(ActivityRuleDao.class);

	/**
	 * 是否初始化缓存
	 */
	private static boolean is_intital = true;

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaDao;

	@Resource(name = "activityRuleDao")
	private IActivityRuleDao activityRleDao;

	/**
	 * 
	 * @Description: 从数据库获取当前活动下所有的活动规则
	 * @param activityId
	 *            活动ID
	 * @return List<ActivityRefRule> 当前活动下活动规则集合
	 * @date 2016年12月7日
	 * @author CHEHUANBO
	 * @since V2.6.2
	 * @Override
	 * 
	 */
	@Transactional(readOnly = true)
	public List<ActivityRefRule> getActivityRuleByActivityIdToDb(String activityId) {

		if (StringUtils.isNotBlank(activityId)) {
			throw new RuntimeException("从数据库获取当前活动下所有的活动规则时出现异常，原因：activityId is null");
		}

		try {

			Criterion criterion = Restrictions.eq("activityId", activityId);
			List<ActivityRefRule> activityRefRuleList = criteriaDao.queryCriteriaList(ActivityRefRule.class, criterion);

			return activityRefRuleList;

		} catch (Exception e) {

			log.error("从数据库获取当前活动下所有的活动规则出现异常！", e);

		}

		return null;

	}

	/**
	 * 
	 * @Description: 从缓存中获取当前活动下的所有活动规则
	 * @param activityId
	 * @return List<ActivityRefRule> 查询到活动规则
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * @Override
	 * 
	 */
	public List<ActivityRefRule> getActivityRuleByActivityIdToCache(String activityId) {

		List<ActivityRefRule> activityRefRuleList = new ArrayList<ActivityRefRule>();

		try {

			// 从缓存中获取
			String activityKey = CacheConstants.ACTIVITY_RULE_LIST_ + activityId;
			byte[] activityRuleBytes = JedisUtil.getValue(SerializableUtil.serialize(activityKey), 3);
			
			// 初始化缓存数据
			if (is_intital) {
				
				activityRefRuleList = activityRleDao.getActivityRuleByActivityIdToDb(activityId);
				JedisUtil.setValue(activityRuleBytes, SerializableUtil.serialize(activityRefRuleList),
						DbEnum.ActivityDb.getValue());
				
				return activityRefRuleList;
				
			}

			// 判断当前活动下的活动规则是否在缓存中存在
			if (JedisUtil.isExistKey(activityRuleBytes, DbEnum.ActivityDb.getValue())) {

				SerializableUtil.deSerialize(activityRuleBytes, activityRefRuleList);
				return activityRefRuleList;

			} else {

				JedisUtil.setValue(activityRuleBytes, SerializableUtil.serialize(activityRefRuleList),
						DbEnum.ActivityDb.getValue());
			}

		} catch (IOException e) {
			log.error("根据活动ID获取当前活动下所有规则出现异常！", e);
		} catch (Exception e) {
			log.error("根据活动ID获取当前活动下所有规则出现异常！", e);
		}

		return activityRefRuleList;

	}

}
